/* Copyright (c) 2006 YourNameHere
   See the file LICENSE.txt for licensing information. */

function debug(string) {
	dump(string + "\n");
}


var fig = {
	
	onUrl: function(evt) {
		var doc = evt.originalTarget;
		
		//debug('doc: ' + doc);
		//debug('document.contentType: ' + doc.contentType);
		//debug('document.documentElement: ' + document.documentElement);
		
		
		var images = doc.images;
		
		if(images.length < 1)
			return;
		
		for(var i = 0; i < images.length; i++) {
			fig.stop(images[i]);
		}
		
	},
	
	stop: function(image) {
		
        var listener = {
          onStartRequest: function(aRequest) {}, 
          onStartDecode: function(aRequest) {},
          onStartContainer: function(aRequest, aContainer) {
            try{
              aContainer.QueryInterface(Components.interfaces.imgIContainer);
			  
			  if(image.getAttribute('play') == 'true') {
				aContainer.animationMode = 0;
			  }
			  else {
				aContainer.animationMode = 1;
			  }
            }
            catch(e) {debug(e)};
          },
          onStartFrame: function(aRequest, aFrame) {},
          onStopFrame: function(aRequest, aFrame) {},
          onStopContainer: function(aRequest, aContainer) {},
          onStopDecode: function(aRequest, status, statusArg) {},
          onStopRequest: function(aRequest, aIsLastPart) {}
        };
		
		try{
          image.QueryInterface(Components.interfaces.nsIImageLoadingContent);
          image.addObserver(listener);
		}
		catch(e) {debug(e);};
		
		image.getRequest(image.CURRENT_REQUEST).clone(listener);
		
		image.addEventListener("click", fig.play, false);
	},
	
	play: function(event) {
		
		var image = event.target;
		
		image.setAttribute('play', !eval(image.getAttribute('play')));
		
		image.forceReload();
	}
}